package com.xl6667.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xl6667.constants.SystemConstants;
import com.xl6667.util.SecurityUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * mybatis字段拦截器
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId = null;
        try {
            userId = SecurityUtil.getUserId();
        } catch (Exception e) {
            e.printStackTrace();
            userId = -1L;
            //表示是自己创建
        }

        this.setFieldValByName("delFlag", SystemConstants.DEL_FLAG_NORMA,metaObject);
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
        this.setFieldValByName("createBy",userId,metaObject);
        this.setFieldValByName("updateBy", userId, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",new Date(),metaObject);
        this.setFieldValByName("updateBy", SecurityUtil.getUserId(), metaObject);
    }
}
